#include <app\session.h>
#include <app\states\sessionState.h>
#include <app\states\statesFactory.h>

using namespace std;

session::session() {
  finish = false;
}

session::~session() {
}

bool
session::addState(sessionState *_state) {
  vector<sessionState *>::iterator istate;

  istate = states.begin();
  for (; istate != states.end(); istate++) {
    if ((*istate)->getTypeId() == _state->getTypeId()) {
      #ifdef DEBUG_INFO
      cerr << "session::addState: state already exists." << endl;
      #endif
      return false;
    }
  }

  _state->setParent(this);
  states.push_back(_state);

  return true;
}

sessionState *
session::getState(int type_id) {
  vector<sessionState *>::iterator istate;
  
  istate = states.begin();
  for (; istate != states.end(); istate++) {
    if ((*istate)->getTypeId() == type_id) {
      return (*istate);
    }
  }
  
  // the state was not found, so feel free to create a new first one
  sessionState *state = statesFactory::instance().createState(type_id);
  state->setParent(this);
  session::addState(state);

  return state;
}

void 
session::requestFinish() {
  finish = true;
}

bool
session::isFinishRequested() {
  return finish;
}
